﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Compression;

namespace MainCableTest.Utils
{
    class Functions
    {
        public Functions()
        { 
        
        }

        public static int PortToRobotIndex(int portnumber)
        {
            switch (portnumber)
            {
                case 100:
                    return 0;
                case 101:
                    return 0;
                case 110:
                    return 1;
                case 111:
                    return 1;
                case 120:
                    return 2;
                case 121:
                    return 2;
                default:
                    return -1;
            }
        }

        public static ushort fastCRC(List<byte> datalist)
        {
            byte[] table = datalist.ToArray();
            const ushort polynomial = 0xA001;

            byte i, chChar;
            ushort wCRC = 0xFFFF;

            int j = 0;
            while (j < table.Length)
            {
                chChar = table[j];
                wCRC ^= chChar;
                for (i = 0; i < 8; i++)
                {
                    if ((wCRC & 0x0001) != 0)
                        wCRC = (ushort)((wCRC >> 1) ^ polynomial);
                    else
                        wCRC >>= 1;
                }

                j++;
            }
            return wCRC;
        }

        public static void PrintArray(byte[] data)
        {
            Console.WriteLine("total size: {0:x}", data.Length);
            for (int i = 0; i < data.Length; i++)
            {
                Console.Write("{0:x} - ", data[i]);
            }
            Console.WriteLine();
        }

        public static void ZipDirectory(string sourceDirectory, string zipFilePath)
        {
            try
            {
                ZipFile.CreateFromDirectory(sourceDirectory, zipFilePath);
            }
            catch(Exception e)
            {
                Console.WriteLine("ZipDirectory occur error, exception: " + e.Message);
            }
        }
    }
}
